package com.home.webapp.base.service.impl;

import com.home.base.core.framework.msg.PageResponse;
import com.home.base.core.framework.msg.ServerResponse;
import com.home.common.framework.core.service.impl.BaseServiceImpl;
import com.home.common.model.TreeNode;
import com.home.common.utils.query.QueryParams;
import com.home.webapp.base.entity.BaseMenu;
import com.home.webapp.base.mapper.BaseMenuMapper;
import com.home.webapp.base.mapper.BaseRoleMenuMapper;
import com.home.webapp.base.service.BaseMenuService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Description: 菜单 serviceImpl
 * Created by 王大宸 on 2020-10-05 23:28:38
 * Created with IntelliJ IDEA.
 */
@Service
public class BaseMenuServiceImpl extends BaseServiceImpl<BaseMenuMapper, BaseMenu, String> implements BaseMenuService {
    private static final Logger logger = LoggerFactory.getLogger(BaseMenuServiceImpl.class);
    @Autowired
    private BaseRoleMenuMapper roleMenuMapper;

    /***
     * 功能说明：分页
     *
     * @author 王大宸
     * @date 2020/11/25 22:33
     * @param params
     * @return com.home.common.framework.msg.PageResponse
     */
    @Override
    public PageResponse<BaseMenu> page(Map<String, Object> params) {
        QueryParams queryParams = new QueryParams(params);
        Page<BaseMenu> page = PageHelper.startPage(queryParams.getPage(), queryParams.getLimit());
        List<BaseMenu> list = baseMapper.page(queryParams);
        return PageResponse.ok(page.getTotal(), list);
    }

    /***
     * 功能说明：获取菜单树(不包含按钮)
     *
     * @author 王大宸
     * @date 2020/10/7 15:42
     * @param
     * @return ServerResponse<List < Map < String, Object>>>
     */
    @Override
    public ServerResponse<List<TreeNode>> zTree() {
        List<TreeNode> list = baseMapper.zTree();
        list.add(new TreeNode("1",
                "请选择应用",
                "0",
                true,
                null,
                "/static/home/zTree/css/zTreeStyle/img/diy/1_open.png",
                "/static/home/zTree/css/zTreeStyle/img/diy/1_close.png")
        );
        // 从数据库获取应用列表
        List<TreeNode> appAdmin = roleMenuMapper.getAppAdmin();
        for (TreeNode treeNode : appAdmin) {
            treeNode.setOpen(true);
            treeNode.setIconOpen("/static/home/zTree/css/zTreeStyle/img/diy/5.png");
            treeNode.setIconClose("/static/home/zTree/css/zTreeStyle/img/diy/5.png");
        }
        list.addAll(appAdmin);
        return ServerResponse.okData(list);
    }
}
